package com.dragon.flow.web.resource;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.dragon.flow.constant.FlowablePlusConstant;
import com.dragon.flow.vo.CheckExistVo;
import com.dragon.tools.common.ReturnCode;
import com.dragon.tools.utils.FastJsonUtils;
import com.dragon.tools.vo.ReturnVo;
import com.ruoyi.common.core.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.reflect.FieldUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @program: flow
 * @description: 基本的资源
 * @author: Bruce.Liu
 * @create: 2021-03-23 21:43
 **/
public abstract class BaseResource<T> extends BaseController {
    protected Logger logger = LoggerFactory.getLogger(this.getClass());
    protected static final String EXIST_MESSAGE = "不能重复！";

    public void authorizationException(HttpServletResponse response) {
        ReturnVo returnVo = new ReturnVo(ReturnCode.FAIL, "您无此功能权限，请联系超级管理员！");
        writeJson(returnVo, response);
    }

    /**
     * 输出JSON
     */
    private void writeJson(ReturnVo returnVo, HttpServletResponse response) {
        PrintWriter out = null;
        try {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("application/json; charset=utf-8");
            out = response.getWriter();
            out.write(FastJsonUtils.objectToJson(returnVo));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
        }
    }
    /**
     * 判断是否唯一
     *
     * @param checkExistVo 参数
     * @return
     */
    protected ReturnVo<Boolean> checkExist(IService<T> service, CheckExistVo checkExistVo) {
        ReturnVo<Boolean> returnVo = new ReturnVo<>(ReturnCode.SUCCESS, "OK", true);
        QueryWrapper<T> userQueryWrapper = new QueryWrapper<>();
        String camelToUnderline = com.baomidou.mybatisplus.core.toolkit.StringUtils.camelToUnderline(checkExistVo.getField());
        userQueryWrapper.eq(camelToUnderline, checkExistVo.getFieldValue());
        userQueryWrapper.eq("del_flag", FlowablePlusConstant.DEL_FLAG_1);
        long count = service.count(userQueryWrapper);
        if (StringUtils.isNotBlank(checkExistVo.getId())) {
            T entity = service.getById(checkExistVo.getId());
            try {
                Object fieldValue = FieldUtils.readField(entity, checkExistVo.getField(), true);
                String oldValue = (String) fieldValue;
                if (!oldValue.equals(checkExistVo.getFieldValue()) && count > 0) {
                    returnVo = new ReturnVo<>(ReturnCode.SUCCESS, checkExistVo.getFieldName() + EXIST_MESSAGE, false);
                }
            } catch (IllegalAccessException e) {
                logger.error("没有相关的字段！字段为：" + checkExistVo.getField());
            }
        } else {
            if (count > 0) {
                returnVo = new ReturnVo<>(ReturnCode.SUCCESS, checkExistVo.getFieldName() + EXIST_MESSAGE, false);
            }
        }
        return returnVo;
    }
}
